<?php
Yii::import('zii.widgets.CMenu');
/**
 * EMultiMenu class file.
 *
 * @require superfish-jquery lib
 * @author Wilson Zeng <jackzcs@gmail.com>
 * @copyright Copyright &copy; 2008-2011 Nirvana-Info .Inc
 */

/**
 * EMultiMenu extends CMenu, so it's compatiable for CMenu usage
 *
 * The following example shows how to use EMultiMenu:
 * <pre>
 * $this->widget('application.extensions.MultiMenu.EMultiMenu', array(
 *     'items'=>array(
 *         // Important: you need to specify url as 'controller/action',
 *         // not just as 'controller' even if default acion is used.
 *         array('label'=>'Home', 'url'=>array('site/index')),
 *         array('label'=>'Products', 'url'=>array('product/index'), 'items'=>array(
 *             array('label'=>'New Arrivals', 'url'=>array('product/new', 'tag'=>'new')),
 *             array('label'=>'Most Popular', 'url'=>array('product/index', 'tag'=>'popular')),
 *         )),
 *         array('label'=>'Login', 'url'=>array('site/login'), 'visible'=>Yii::app()->user->isGuest),
 *     ),
 * ));
 * </pre>
 */
class EMultiMenu extends CMenu
{
	public $activeCssClass = 'sfHover';
	public $encodeLabel = false;

	public function init(){
		parent::init();
		$dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'superfish';
		$baseUrl =Yii::app()->getAssetManager()->publish($dir, false, -1, YII_DEBUG);
		$cs = Yii::app()->getClientScript();
		$cs->registerCoreScript('jquery');
		//$cs->registerScriptFile($baseUrl.'/js/hoverIntent.js');
		$cs->registerScriptFile($baseUrl.'/js/superfish.js');
		$cs->registerCssFile($baseUrl.'/css/superfish.css');
		$cs->registerCssFile($baseUrl.'/css/superfish-navbar.css');
		isset($this->htmlOptions['class']) or $this->htmlOptions['class'] = '';
		$this->htmlOptions['class'] .= ' sf-menu sf-navbar';
		$id = $this->getId();
		$defaultScript = <<<EOS
$(document).ready(function(){
	$("#$id").superfish();
});
EOS;
		$cs->registerScript($this->getId(), $defaultScript);
	}
	
	protected function renderMenu($items)
	{
		if(count($items))
		{
			echo CHtml::openTag('div', array('id'=>'superfishmenu'))."\n";
			echo CHtml::openTag('ul',$this->htmlOptions)."\n";
			$this->renderMenuRecursive($items);
			echo CHtml::closeTag('ul');
			echo CHtml::closeTag('div');
			echo CHtml::openTag('div', array('style'=>'clear:both;'))."\n";
			echo CHtml::closeTag('div');
		}
	}
	
	protected function renderMenuRecursive($items)
	{
		$count=0;
		$n=count($items);
		foreach($items as $item)
		{
			//zcs=>extra default css style & a img arrow for superfish
			if(isset($item['items']) && count($item['items']))
				$item['label'] .= '<span class="sf-sub-indicator"> »</span>';
			isset($item['linkOptions']['class']) or $item['linkOptions']['class'] = '';
			$item['linkOptions']['class'] .= ' sf-with-ul';
			//<=zcs
			$count++;
			$options=isset($item['itemOptions']) ? $item['itemOptions'] : array();
			$class=array();
			if($item['active'] && $this->activeCssClass!='')
				$class[]=$this->activeCssClass;
			if($count===1 && $this->firstItemCssClass!='')
				$class[]=$this->firstItemCssClass;
			if($count===$n && $this->lastItemCssClass!='')
				$class[]=$this->lastItemCssClass;
			if($class!==array())
			{
				if(empty($options['class']))
					$options['class']=implode(' ',$class);
				else
					$options['class'].=' '.implode(' ',$class);
			}

			echo CHtml::openTag('li', $options);

			$menu=$this->renderMenuItem($item);
			if(isset($this->itemTemplate) || isset($item['template']))
			{
				$template=isset($item['template']) ? $item['template'] : $this->itemTemplate;
				echo strtr($template,array('{menu}'=>$menu));
			}
			else
				echo $menu;

			if(isset($item['items']) && count($item['items']))
			{
				echo "\n".CHtml::openTag('ul',isset($item['submenuOptions']) ? $item['submenuOptions'] : $this->submenuHtmlOptions)."\n";
				$this->renderMenuRecursive($item['items']);
				echo CHtml::closeTag('ul')."\n";
			}

			echo CHtml::closeTag('li')."\n";
		}
	}
}